package cn.swust.service.serviceimpl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import cn.swust.model.UserDonorBean;
import cn.swust.service.UserDonorService;
import cn.swust.util.DBUtil;

public class UserDonorServiceImpl implements UserDonorService {

	public int addDonor(UserDonorBean donor) {
		int ret = 0;
		try{
		String insertSql = "insert into user_donor(donor_id,user_id) values (" + 
		donor.getDonorId() + "," + donor.getUserId() + ")";
		Connection conn = DBUtil.getConnection();
		Statement state = conn.createStatement();
		state.execute(insertSql, Statement.RETURN_GENERATED_KEYS);
		ResultSet rs = state.getGeneratedKeys();
		while(rs.next())
			ret = rs.getInt(1);
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		return ret;
	}

	public int delDonor(int id) {
		String delSql = "delete from user_donor where user_donor_id = " + id;
		return DBUtil.updateSql(delSql);
	}

	public int modDonor(int id, UserDonorBean donor) {
		String modSql = "update user_donor set donor_id = " + donor.getDonorId() +
		",user_id = " + donor.getUserId() + " where user_donor_id = " + id;
		return DBUtil.updateSql(modSql);
	}

	public UserDonorBean queryById(int id) {
		String insertSql = "select * from user_donor where user_donor_id = " + id;
		ResultSet rs = DBUtil.querySql(insertSql, DBUtil.creatStatement(DBUtil.getConnection()));
		UserDonorBean donor = new UserDonorBean(); 
		try {
			if (rs.next()) {
				donor.setId(rs.getInt("user_donor_id"));
				donor.setDonorId(rs.getInt("donor_id"));
				donor.setUserId(rs.getInt("user_id"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return donor;
	}

	public ArrayList<UserDonorBean> queryByProId(int proId) {
		String insertSql = "select * from user_donor where donor_id = " + proId;
		ResultSet rs = DBUtil.querySql(insertSql, DBUtil.creatStatement(DBUtil.getConnection()));
		ArrayList<UserDonorBean> donorList = new ArrayList<UserDonorBean>();
		try {
			while (rs.next()) {
				UserDonorBean donor = new UserDonorBean(); 
				donor.setId(rs.getInt("user_donor_id"));
				donor.setDonorId(rs.getInt("donor_id"));
				donor.setUserId(rs.getInt("user_id"));
				donorList.add(donor);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return donorList;
	}

	public ArrayList<UserDonorBean> queryByUserId(int userId) {
		String insertSql = "select * from user_donor where user_id = " + userId;
		ResultSet rs = DBUtil.querySql(insertSql, DBUtil.creatStatement(DBUtil.getConnection()));
		ArrayList<UserDonorBean> donorList = new ArrayList<UserDonorBean>();
		try {
			while (rs.next()) {
				UserDonorBean donor = new UserDonorBean(); 
				donor.setId(rs.getInt("user_donor_id"));
				donor.setDonorId(rs.getInt("donor_id"));
				donor.setUserId(rs.getInt("user_id"));
				donorList.add(donor);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return donorList;
	}

}
